using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Timers;

using MediaPortal.GUI.Library;
using MediaPortal.Dialogs;
using MediaPortal.Util;
using MediaPortal.Localisation;
using MediaPortal.Configuration;
using MediaPortal.Player;
using MediaPortal.Playlists;

using YahooMusicEngine;
using YahooMusicEngine.OnlineDataProvider;
using YahooMusicEngine.Services;
using YahooMusicEngine.Entities;
using YahooMusicEngine.Locale;

namespace YahooMusicPlugin
{

  public class YahooMusicInfoGUI : YahooMusicGUIBase
  {
    #region skin connection
    [SkinControlAttribute(50)]
    protected GUIListControl catListControl = null;
    [SkinControlAttribute(51)]
    protected GUIListControl similarListControl = null;
    [SkinControlAttribute(5)]
    protected GUIButtonControl btnPlay = null;

    #endregion
    public VideoResponse InfoItem;
    public override int GetID
    {
      get
      {
        return 27051;
      }

      set
      {
      }
    }

    public YahooMusicInfoGUI()
    {

    }

    public override bool Init()
    {
      provider.AppId = "DeUZup_IkY7d17O2DzAMPoyxmc55_hTasA--";
      provider.SharedSecret = "d80b9a5766788713e1fadd73e752c7eb";
      provider.Init();
      return Load(GUIGraphicsContext.Skin + @"\yahoomusicinfo.xml");
    }

    protected override void OnPageLoad()
    {
      _setting.Load();
      if (InfoItem != null && !string.IsNullOrEmpty(InfoItem.Video.Id))
      {
        GUIPropertyManager.SetProperty("#header.label", GUILocalizeStrings.Get(127075));
        Log.Debug("info for {0} {1}", InfoItem.Video.Title, InfoItem.Categories.Count.ToString());
        SimilarVideosService similar = new SimilarVideosService();
        similar.Id = InfoItem.Video.Id;
        provider.GetData(similar);
        SetLabels(InfoItem, "Curent");
        GUIControl.ClearControl(GetID, catListControl.GetID);
        foreach (CategoryEntity cat in InfoItem.Categories)
        {
          GUIListItem item = new GUIListItem();
          // and add station name & bitrate
          item.Label = cat.Name;
          item.Label2 = "";
          item.IsFolder = true;
          item.MusicTag = cat;
         // MediaPortal.Util.Utils.SetDefaultIcons(item);
          catListControl.Add(item);
        }
        addVideos(similar.Items, similarListControl);
      }
      base.OnPageLoad();
    }

    protected override void OnPageDestroy(int new_windowId)
    {
      base.OnPageDestroy(new_windowId);
    }

    void addVideos(List<VideoResponse> vids, GUIListControl listControl)
    {
      foreach (VideoResponse vi in vids)
      {
        if (filterVideoContens(vi))
        {
          GUIListItem item = new GUIListItem();
          // and add station name & bitrate
          item.Label = FormatTitle(vi);
          item.Label2 = "";
          item.IsFolder = false;
          item.ThumbnailImage = GetLocalImageFileName(vi.Image.Url);
          item.IconImage = "defaultVideoBig.png";
          item.IconImageBig = GetLocalImageFileName(vi.Image.Url);
          item.Year = vi.Video.CopyrightYear;
          item.Duration = vi.Video.Duration;
          item.Rating = vi.Video.Rating;
          item.OnItemSelected += new GUIListItem.ItemSelectedHandler(item_OnItemSelected);
          //DownloadImage(vi);
          item.MusicTag = vi;
          listControl.Add(item);
        }
      }
    }

    void item_OnItemSelected(GUIListItem item, GUIControl parent)
    {
//      throw new Exception("The method or operation is not implemented.");
    }

    protected override void OnClicked(int controlId, GUIControl control, Action.ActionType actionType)
    {
      if (control == btnPlay)
      {
        DoPlay(InfoItem);
      }
      else if (control == similarListControl)
      {
        VideoResponse item = similarListControl.SelectedListItem.MusicTag as VideoResponse;
        DoPlay(item);
      }
      else if (control == catListControl)
      {
        CategoryTreeService cat = new CategoryTreeService();
        CategoryEntity item = catListControl.SelectedListItem.MusicTag as CategoryEntity;
        cat.RootID = item.Id;
        cat.Type = item.Type;
        provider.GetData(cat);
        item.Childs = cat.Items;
        MessageGUI.Item = cat.Items[0];
        GUIWindowManager.ShowPreviousWindow();
      }

      base.OnClicked(controlId, control, actionType);
    }

      }
}
